<?php
/*
 +----------------------------------------------------------------------
 + Title        : Where 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2019/5/27 8:33
 + Last-time    : 2019/5/27 8:33+ 86187
 + Desc         : Where
 +----------------------------------------------------------------------
*/


namespace lib;

use think\db\Query;
use think\Exception;
use think\Paginator;
use think\facade\Request;

class MyQuery extends Query {
    /**
     * 设置Like查询条件
     *
     * @param string|array $fields 查询字段
     * @param string       $input  输入类型 get|post
     * @param string       $alias  别名分割符
     *
     * @return MyQuery
     */
    public function like($fields, $input = 'request', $alias = '#') {
        $data = is_array($input) ? $input : Request::$input();
        foreach (is_array($fields) ? $fields : explode(',', $fields) as $field) {
            [
                $dk,
                $qk
            ] = [
                $field,
                $field
            ];
            if (stripos($field, $alias) !== false) {
                [
                    $dk,
                    $qk
                ] = explode($alias, $field);
            }
            if (isset($data[$qk]) && $data[$qk] !== '') {
                $this->where($dk, 'LIKE', "%{$data[$qk]}%");
            }
        }
        return $this;
    }

    /**
     * 设置Equal查询条件
     *
     * @param string|array $fields 查询字段
     * @param string|array $input  输入类型 get|post
     * @param string       $alias  别名分割符
     *
     * @return $this
     */
    public function equal($fields, $input = 'param', $alias = '#') {
        $data = is_array($input) ? $input : Request::$input();

        foreach (is_array($fields) ? $fields : explode(',', $fields) as $field) {
            [
                $dk,
                $qk
            ] = [
                $field,
                ''
            ];
            if (stripos($field, $alias) !== false) {
                [
                    $dk,
                    $qk
                ] = explode($alias, $field);
            }

            if (isset($data[$dk]) && $data[$dk] !== '') {
                if ($qk) {
                    $this->where($qk . '.' . $dk, '=', "{$data[$dk]}");
                } else {
                    $this->where($dk, '=', "{$data[$dk]}");
                }
            }
        }
        return $this;
    }

    /**
     * 设置Equal查询条件
     *
     * @param string|array $fields 查询字段
     * @param string|array $input  输入类型 get|post
     * @param string       $alias  别名分割符
     *
     * @return $this
     */
    public function lt($fields, $input = 'param', $alias = '#') {
        $data = is_array($input) ? $input : Request::$input();

        foreach (is_array($fields) ? $fields : explode(',', $fields) as $field) {
            [
                $dk,
                $qk
            ] = [
                $field,
                ''
            ];
            if (stripos($field, $alias) !== false) {
                [
                    $dk,
                    $qk
                ] = explode($alias, $field);
            }

            if (isset($data[$dk]) && $data[$dk] !== '') {
                if ($qk) {
                    $this->where($dk . '.' . $dk, '<', "{$data[$dk]}");
                } else {
                    $this->where($dk, '<', "{$data[$dk]}");
                }
            }
        }
        return $this;
    }


    /**
     * 设置Equal查询条件
     *
     * @param string|array $fields 查询字段
     * @param string|array $input  输入类型 get|post
     * @param string       $alias  别名分割符
     *
     * @return $this
     */
    public function gt($fields, $input = 'param', $alias = '#') {
        $data = is_array($input) ? $input : Request::$input();

        foreach (is_array($fields) ? $fields : explode(',', $fields) as $field) {
            [
                $dk,
                $qk
            ] = [
                $field,
                ''
            ];
            if (stripos($field, $alias) !== false) {
                [
                    $dk,
                    $qk
                ] = explode($alias, $field);
            }

            if (isset($data[$dk]) && $data[$dk] !== '') {
                if ($qk) {
                    $this->where($dk . '.' . $dk, '>', "{$data[$dk]}");
                } else {
                    $this->where($dk, '>', "{$data[$dk]}");
                }
            }
        }
        return $this;
    }


    /**
     * 设置IN区间查询
     *
     * @param string $fields 查询字段
     * @param string $split  输入分隔符
     * @param string $input  输入类型 get|post
     * @param string $alias  别名分割符
     *
     * @return $this
     */
    public function in($fields, $split = ',', $input = 'param', $alias = '#') {
        $data = is_array($input) ? $input : Request::$input();
        foreach (is_array($fields) ? $fields : explode(',', $fields) as $field) {
            [
                $dk,
                $qk
            ] = [
                $field,
                $field
            ];
            if (stripos($field, $alias) !== false) {
                [
                    $dk,
                    $qk
                ] = explode($alias, $field);
            }

            if (isset($data[$dk]) && $data[$dk] !== '') {
                if ($qk) {
                    $this->where($qk . '.' . $dk, 'IN', explode($split, $data[$qk]));
                } else {
                    $this->where($dk, 'IN', explode($split, $data[$qk]));
                }
            }
        }
        return $this;
    }

    /**
     * 设置内容区间查询
     *
     * @param string|array $fields 查询字段
     * @param string       $split  输入分隔符
     * @param string       $input  输入类型 get|post
     * @param string       $alias  别名分割符
     *
     * @return $this
     */
    public function valueBetween($fields, $split = ' ', $input = 'param', $alias = '#') {

        return $this->setBetweenWhere($fields, $split, $input, $alias);
    }


    /**
     * 设置日期时间区间查询
     *
     * @param string|array $fields 查询字段
     * @param string       $split  输入分隔符
     * @param string       $input  输入类型
     * @param string       $alias  别名分割符
     *
     * @return $this
     */
    public function dateBetween($fields, $split = ' - ', $input = 'param', $alias = '#') {
        return $this->setBetweenWhere($fields, $split, $input, $alias, function ($value, $type) {
            if ($type === 'after') {
                return "{$value} 23:59:59";
            } else {
                return "{$value} 00:00:00";
            }
        });
    }

    /**
     * 设置时间戳区间查询
     *
     * @param string|array $fields 查询字段
     * @param string       $split  输入分隔符
     * @param string       $input  输入类型
     * @param string       $alias  别名分割符
     *
     * @return $this
     */
    public function timeBetween($fields, $split = ' - ', $input = 'param', $alias = '#') {
        return $this->setBetweenWhere($fields, $split, $input, $alias, function ($value, $type) {
            if ($type === 'after') {
                return strtotime("{$value} 23:59:59");
            } else {
                return strtotime("{$value} 00:00:00");
            }
        });
    }


    /**
     * 设置区域查询条件
     *
     * @param string|array $fields 查询字段
     * @param string       $split  输入分隔符
     * @param string       $input  输入类型
     * @param string       $alias  别名分割符
     * @param callable     $callback
     *
     * @return $this
     */
    private function setBetweenWhere($fields, $split = ' ', $input = 'param', $alias = '#', $callback = null) {
        $data = is_array($input) ? $input : Request::$input();

        foreach (is_array($fields) ? $fields : explode(',', $fields) as $field) {
            [
                $dk,
                $qk
            ] = [
                $field,
                ''
            ];
            if (stripos($field, $alias) !== false) {
                [
                    $dk,
                    $qk
                ] = explode($alias, $field);
            }
            if (isset($data[$dk]) && $data[$dk]) {
                [
                    $begin,
                    $after
                ] = explode($split, $data[$dk]);
                if (is_callable($callback)) {
                    $after = call_user_func($callback, $after, 'after');
                    $begin = call_user_func($callback, $begin, 'begin');
                }

                $this->where($qk ? $qk . '.' . $dk : $dk, 'between', [
                    $begin,
                    $after
                ]);


            }
        }
        return $this;
    }

    public function setOrder($fields, $split = ',') {
        $order = explode($split, $fields);
        foreach ($order as $v) {
            $this->order($v);
        }
        return $this;
    }

    public function setLimit($limit) {
        $this->limit($limit);
        return $this;
    }

    /**
     * 设置读取字段
     *
     * @param        $fields
     * @param string $alias
     *
     * @return $this
     *
     *
     * @author : Randy_chen
     * @Date   : 2019/7/5
     * @Time   : 21:08
     */
    public function setFields($fields, $alias = '#') {
        $_fields = [];
        foreach (is_array($fields) ? $fields : explode(',', $fields) as $field) {
            [
                $dk,
                $qk
            ] = [
                $field,
                ''
            ];
            if (stripos($field, $alias) !== false) {
                [
                    $dk,
                    $qk
                ] = explode($alias, $field);
            }
            if ($qk) {
                $_fields[] = [$qk . '.' . $dk];
            } else {
                $_fields[] = [$dk];
            }
        }
        $_fields && $this->field($_fields);
        return $this;
    }


    /**
     * 分页查询
     * @access public
     * @param int|array $listRows 每页数量 数组表示配置参数
     * @param int|bool  $simple   是否简洁模式或者总记录数
     *
     * @return Paginator
     * @throws \think\exception\DbException
     */
    public function _paginate($listRows = null, $simple = false) : Paginator {
        $defaultConfig = [
            'query'     => [],
            //url额外参数
            'fragment'  => '',
            //url锚点
            'var_page'  => 'page',
            //分页变量
            'list_rows' => 15,
            //每页数量
        ];
        $arr = [];
        \request()->param(config('paginate.var_limit')) && $arr['list_rows'] = request()->param(config('paginate.var_limit'));

        if (config('paginate.')) {
            $defaultConfig = array_merge($defaultConfig, config('paginate.'), $arr);
        }

	    return $this->paginate($defaultConfig, $simple);

    }
}